﻿
package bsplib;

class Vec2f
{
    public var x : Float;
    public var y : Float;
    
    public function new( x : Float, y : Float ) 
    {
        this.x = x;
        this.y = y;
    }
    
    public inline function clone() : Vec2f {
        return new Vec2f(x, y);
    }
    
    public inline function copy( v : Vec2f ) : Vec2f {
        x = v.x;
        y = v.y;
        return this;
    }
    
    public function toString() : String {
        return "<Vec2f " + x + ", " + y + " >";
    }
    
    public inline function add( v : Vec2f ) : Vec2f {
        x += v.x;
        y += v.y;
        return this;
    }
    
    public inline function cAdd( v1 : Vec2f, v2 : Vec2f ) : Vec2f {
        x = v1.x + v2.x;
        y = v1.y + v2.y;
        return this;
    }
    
    public inline function sub( v : Vec2f ) : Vec2f {
        x -= v.x;
        y -= v.y;
        return this;
    }
    
    public inline function cSub( v1 : Vec2f, v2 : Vec2f ) : Vec2f {
        x = v1.x - v2.x;
        y = v1.y - v2.y;
        return this;
    }
    
    public inline function dot( v : Vec2f ) : Float {
        return x * v.x + y * v.y;
    }
    
    public inline function lengthSqr() : Float {
        return x * x + y * y;
    }
    
    public inline function length() : Float {
        return Math.sqrt( lengthSqr() );
    }
    
    public inline function normalize() : Vec2f {
        var len = length();
        x /= len;
        y /= len;
        return this;
    }
    
    public inline function cNormal( v : Vec2f ) : Vec2f {
        var len = v.length();
        x = v.x / len;
        y = v.y / len;
        return this;
    }
    
    public inline function scale( s : Float ) : Vec2f {
        x *= s;
        y *= s;
        return this;
    }
    
    public inline function cScale( v : Vec2f, s : Float ) : Vec2f {
        x = v.x * s;
        y = v.y * s;
        return this;
    }
    
    public inline function negate() : Vec2f {
        x = -x; y = -y;
        return this;
    }
    
    public inline function cNegated( v : Vec2f ) : Vec2f {
        x = -v.x; y = -v.y;
        return this;
    }
    
    public inline function interpolate( v : Vec2f, f : Float ) : Vec2f {
        x += ( v.x - x ) * f;
        y += ( v.y - y ) * f;
        return this;
    }
    
    public inline function cInterpolate( v1 : Vec2f, v2 : Vec2f, f : Float ) : Vec2f {
        x = v1.x + ( v2.x - v1.x ) * f;
        y = v1.y + ( v2.y - v1.y ) * f;
        return this;
    }
    
}